package controler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.CRN;
import model.SubjectContent;
import model.beans.CRNBean;
import model.beans.UsuarioBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import business.misc.Util;

public class SubjectDateAction extends MappingDispatchAction {

	/* forward name="default" path="/subject_cListPlannedDate.jsp" */

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		ArrayList arrL_Units = null;
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;				
		String str_crn = null;
		String str_idUser = null;
		
		String str_crncode = request.getParameter("crn_code");
    	String str_ciclo = request.getParameter("crn_ciclo"); 
    	
		CRNBean crn_bean = CRN.instance(str_crncode, str_ciclo);
		String materia_tk = crn_bean.getMateria_tk();
		HttpSession session = request.getSession();    	
    	UsuarioBean user_log = (UsuarioBean) session.getAttribute("ses_usuario");         	
    	str_crn = request.getParameter("crn");
		if(str_crn.equalsIgnoreCase("alumno")){
			str_idUser = crn_bean.getMaestro_tk();		
		}else if(str_crn.equalsIgnoreCase("maestro")){
			str_idUser = user_log.getId();
		}    	   					   		   

    	arrL_Units = SubjectContent.list(materia_tk);
    	SubjectContent.getDates(materia_tk, str_idUser, arrL_Units);
    	request.setAttribute("lst_unitContent", arrL_Units);
    	//request.setAttribute("req_crns", CRN.list(str_idUser, CRN.TEACHER));
    	request.setAttribute("crn_bean", crn_bean);
    	
    	return mapping.findForward(Util.DEFAULT); 
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String str_idUser = null;
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		int cont = 0;
		String fecha = null;
		String subtopic = null;
		String dateType = null;	
		String materia_tk = null;
		ArrayList arrL_Units = null;
		Map mapa = new HashMap();
		
		String str_type = request.getParameter("type");
		String str_crncode = request.getParameter("crn_code");
    	String str_ciclo = request.getParameter("crn_ciclo");
    	
    	HttpSession session = request.getSession();
		UsuarioBean user_log = (UsuarioBean) session.getAttribute("ses_usuario");
    	str_idUser = user_log.getId();
    	
    	CRNBean crn_bean = CRN.instance(str_crncode, str_ciclo);
		materia_tk = crn_bean.getMateria_tk();
		
		
		if(str_type.equalsIgnoreCase("ACTUAL")){
			dateType = "str_actualDate";
		}else if(str_type.equalsIgnoreCase("PLANNED")){
			dateType = "str_plannedDate";
		}
		
		do{
			fecha=request.getParameter(dateType + cont);
			subtopic = request.getParameter("subtopic" + cont);
			if(fecha != null && subtopic != null){
					mapa.put(dateType + cont, fecha);	
					mapa.put("subtopic" +cont, subtopic);			
					cont++;
			}
		}while(fecha != null && subtopic!=null );				    	    	    		
		
    	if(SubjectContent.saveDate(mapa, str_idUser, str_type) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
			request.setAttribute("req_crns", CRN.list(str_idUser, CRN.TEACHER));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
		}
    	arrL_Units = SubjectContent.list(materia_tk);
    	SubjectContent.getDates(materia_tk, str_idUser, arrL_Units);
    	request.setAttribute("lst_unitContent", arrL_Units);    	
    	request.setAttribute("crn_bean", crn_bean);
					
    	return mapping.findForward(Util.DEFAULT); 
	}
}
